package com.example.mac.yuejian14.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import com.example.mac.yuejian14.R;


public class ActivityFragment extends Fragment {
    private RollPagerView rollPV;
    // 声明变量
    private View view = null;
    private ImageView imagesearch;//搜索
    private RadioButton wonderRb;
    private RadioButton teacherRb;
    private RadioButton matchRb;
    private WonderFragment wonderFragment;
    private TeacherFragment teacherFragment;
    private MatchFragment matchFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;


    public ActivityFragment(){

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.fragment_activity, container, false);
        initViews();
        // getData();
        initEvents();
        return view;
    }

    private void initViews(){
//        imagesearch = (ImageView) view.findViewById(R.id.shouye_pic);
        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        wonderRb = (RadioButton) view.findViewById(R.id.shouye_wonder);
        teacherRb = (RadioButton) view.findViewById(R.id.shouye_teacher);
        matchRb = (RadioButton) view.findViewById(R.id.shouye_match);

        imagesearch=(ImageView) view.findViewById(R.id.imagesearch);

    }
    public void initEvents() {
        //首页三个列表
        //精选活动页
        wonderRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    wonderFragment = new WonderFragment();
                    replaceFragment(wonderFragment);
                }
            }
        });
        //活动大师
        teacherRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    teacherFragment = new TeacherFragment();
                    replaceFragment(teacherFragment);
                }

            }
        });
        matchRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    matchFragment = new MatchFragment();
                    replaceFragment(matchFragment);
                }
            }
        });
        fragmentManager=getChildFragmentManager();
        wonderRb.setChecked(true);


    }
    private void replaceFragment(Fragment fragment) {
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder,fragment);
        transaction.commit();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        //设置每个图片的切换时间
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:

        rollPV.setAdapter(new RollPagerAdapter());

    }

    //适配器
    private class RollPagerAdapter extends StaticPagerAdapter {

        private int images[] = {
                R.drawable.img1,
                R.drawable.img2,
                R.drawable.img3,
                R.drawable.img4
        };

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {

            return images.length;
        }
    }



}
